import { mockSetup } from '@/utils/mockSetup'
import qs from 'query-string'
import Mock from 'mockjs'

mockSetup({
  mock: true,
  setup: () => {
    Mock.mock('/api/workplace/overview-content', () => {
      const year = new Date().getFullYear()
      const getLineData = () => {
        return new Array(12).fill(0).map((_item, index) => ({
          date: `${year}-${index + 1}`,
          count: Mock.Random.natural(20000, 75000)
        }))
      }
      return {
        message: 'success',
        status: 0,
        data: {
          allContents: '373.5w+',
          liveContents: '368',
          increaseComments: '8874',
          growthRate: '2.8%',
          chartData: getLineData()
        }
      }
    })
    const getList = () => {
      const { list } = Mock.mock({
        'list|100': [
          {
            'rank|+1': 1,
            title: '@ctitle',
            type: 0,
            increase: '@natural(0, 100)',
            pv: '@natural(1000, 10000)'
          }
        ]
      })
      return list
    }
    const listText = getList()
    const listPic = getList()
    const listVideo = getList()
    Mock.mock(new RegExp('/api/workplace/popular-contents'), (params) => {
      const {
        page = 1,
        pageSize = 5,
        category = 0
      } = qs.parseUrl(params.url).query as unknown as {
        page?: number
        pageSize?: number
        category?: number
      }

      const list: Array<any> = [listText, listPic, listVideo][Number(category)] || []
      return {
        status: 0,
        message: 'success',
        data: {
          list: list.slice((page - 1) * pageSize, page * pageSize),
          total: 100
        }
      }
    })
    Mock.mock('/api/workplace/announcement', () => {
      const { list } = Mock.mock({
        'list|5': [
          {
            'type|1': ['activity', 'info', 'notice'],
            'key|+1': 1,
            content: '@ctitle(5, 10)'
          }
        ]
      })
      return list
    })
  }
})
